﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace IronWinter
{
    public class MenuButton
    {
        public static SpriteFont menuFont;
        public enum ButtonAction { MainMenu, CustomMenu, AddUnit, EditUnit, DupUnit, RemUnit, SaveUnits, SettingsMenu, FullScreen, Play, Resume, Quit, ActivateUnit, None, Upgrade,
        spawnLightI, spawnMedI, spawnHeavyI, spawnLightII, spawnMedII, spawnHeavyII, HostGame, FindGame, joingame0, joingame1, joingame2, NetworkMenu, InitializeNetworkGame
        }

        public const int width = 250, height = 50;
        public const int cwidth = 250, cheight = 100;
        public Rectangle bounds;
        public Vector2 position;
        public Boolean isSelected;
        private String name;
        private Texture2D texture;
        public UnitSpecs unit;
        private int X, Y;
        public ButtonAction action;

        public Weapon.types weapon = Weapon.types.none;
        public Armor.types ar = Armor.types.none;
        public Item.types item = Item.types.none;
        public int ability;
        public List<Ability> abilities;

        public string title
        {
            get { return name; }
            set { name = value; }

        }


        public MenuButton(String startName, ButtonAction act, Vector2 startPos, Texture2D tex)
        {
            action = act;
            isSelected = false;
            name = startName;
            position = startPos;
            texture = tex;
            X = (int)startPos.X;
            Y = (int)startPos.Y;
            bounds = new Rectangle(X, Y, width, height);
        }

        public MenuButton(UnitSpecs us, ButtonAction act, Vector2 startPos, Texture2D tex)
        {
            action = act;
            isSelected = false;
            name = us.name + " Cost: " + (us.cost + 60);
            position = startPos;
            texture = tex;
            X = (int)startPos.X;
            Y = (int)startPos.Y;
            bounds = new Rectangle(X, Y, width, height);
        }

        public MenuButton(UnitSpecs us, Vector2 startPos, Texture2D tex)
        {
            texture = tex;
            unit = us;
            abilities = new List<Ability>();

            foreach (int i in us.abilities)
                abilities.Add(Ability.AbilityList[i]);

            X = (int)startPos.X;
            Y = (int)startPos.Y;
            action = ButtonAction.None;
            bounds = new Rectangle(X, Y, cwidth, cheight);
        }

        public MenuButton(Vector2 startPos, Texture2D tex)
        {
            isSelected = false;
            texture = tex;
            X = (int)startPos.X;
            Y = (int)startPos.Y;
            action = ButtonAction.Upgrade;
            bounds = new Rectangle(X, Y, Ability.DISPLAY_SIZE, Ability.DISPLAY_SIZE);
        }

        public MenuButton(Vector2 startPos, Texture2D tex,Weapon.types w, Armor.types a, Item.types i, int ab)
        {
            weapon = w;
            ar = a;
            item = i;
            ability = ab;

            isSelected = false;
            texture = tex;
            X = (int)startPos.X;
            Y = (int)startPos.Y;
            action = ButtonAction.Upgrade;
            bounds = new Rectangle(X, Y, Ability.DISPLAY_SIZE, Ability.DISPLAY_SIZE);
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(texture, bounds, isSelected ? Color.Cyan : Color.White);

            
            if (action == ButtonAction.None)
            {
                int port = 0;
                if (unit.type == Unit.UnitType.Medium)
                    port = 1;
                else if (unit.type == Unit.UnitType.Light)
                    port = 2;

                sb.Draw(Unit.Portraits[port], new Rectangle(X + 10, Y + 5, Unit.PORTRAIT_WIDTH / 2, Unit.PORTRAIT_HEIGHT / 2), Color.White);

                int itemstart = X + 20 + Unit.PORTRAIT_WIDTH / 2;

                sb.DrawString(MenuButton.menuFont, unit.name, new Vector2(itemstart, Y + 5), Color.Red, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0);

                //Display abilities 
                for (int u = 0; u < unit.abilities.Count; u++)
                    Ability.AbilityList[unit.abilities[u]].Display(sb, new Rectangle(itemstart + u * (Ability.DISPLAY_SIZE / 2 + 5), Y + 25, Ability.DISPLAY_SIZE / 2, Ability.DISPLAY_SIZE / 2));

                //Display equipment

                EquipmentLibrary.getWeapon(unit.weapon).Display(sb, new Rectangle(itemstart + 0 * (Equipment.DISPLAY_SIZE / 2 + 5), Y + 25 + Equipment.DISPLAY_SIZE / 2 + 5, Equipment.DISPLAY_SIZE / 2, Equipment.DISPLAY_SIZE / 2));
                EquipmentLibrary.getArmor(unit.ar).Display(sb, new Rectangle(itemstart + 1 * (Equipment.DISPLAY_SIZE / 2 + 5), Y + 25 + Equipment.DISPLAY_SIZE / 2 + 5, Equipment.DISPLAY_SIZE / 2, Equipment.DISPLAY_SIZE / 2));
                EquipmentLibrary.getItem(unit.item).Display(sb, new Rectangle(itemstart + 2 * (Equipment.DISPLAY_SIZE / 2 + 5), Y + 25 + Equipment.DISPLAY_SIZE / 2 + 5, Equipment.DISPLAY_SIZE / 2, Equipment.DISPLAY_SIZE / 2));

                sb.Draw(texture, new Rectangle(itemstart + 3 * (Equipment.DISPLAY_SIZE / 2 + 5), Y + 25, Ability.DISPLAY_SIZE / 2, Ability.DISPLAY_SIZE), unit.isActive ? Color.Green : Color.Red);
            }
            else if (action == ButtonAction.Upgrade)
            {
                sb.Draw(texture, bounds, isSelected ? Color.Cyan : Color.White);
            }
            else
            {
                Vector2 fontPos = new Vector2(position.X + width / 2, position.Y + height / 2);
                Vector2 origin = new Vector2(menuFont.MeasureString(name).X / 2, menuFont.MeasureString(name).Y / 2);
                sb.DrawString(menuFont, name, fontPos + Vector2.One, Color.White, 0f, origin, 1f, SpriteEffects.None, 1f);
                sb.DrawString(menuFont, name, fontPos + new Vector2(-1, -1), Color.White, 0f, origin, 1f, SpriteEffects.None, 1f);
                sb.DrawString(menuFont, name, fontPos + new Vector2(-1, 1), Color.White, 0f, origin, 1f, SpriteEffects.None, 1f);
                sb.DrawString(menuFont, name, fontPos + new Vector2(1, -1), Color.White, 0f, origin, 1f, SpriteEffects.None, 1f);

                sb.DrawString(menuFont, name, fontPos, Color.Black, 0f, origin, 1f, SpriteEffects.None, 1f);
            }
        }
    }
}
